package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.IAgreementService;
import com.ibeeking.gmc.global.service.common.dos.AgreementDO;
import com.ibeeking.gmc.global.service.common.param.AgreementParam;
import com.ibeeking.gmc.global.service.common.query.AgreementPageQuery;
import com.ibeeking.gmc.global.service.common.vo.AgreementVO;
import com.ibeeking.gmc.global.service.mapper.AgreementMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class AgreementServiceImpl extends ServiceImpl<AgreementMapper, AgreementDO> implements IAgreementService {

    @Resource
    private AgreementMapper agreementMapper;


    @Override
    public AgreementVO view(Long id) {
        AgreementVO vo = BeanUtil.convertBean(this.agreementMapper.selectById(id), AgreementVO.class);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AgreementParam agreementParam) {
        AgreementDO agreementDO = BeanUtil.convertBean(agreementParam, AgreementDO.class);
        agreementDO.setCreateTime(LocalDateTime.now());
        agreementDO.setModifyTime(LocalDateTime.now());
        return this.agreementMapper.insert(agreementDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(AgreementParam agreementParam) {
        AgreementDO agreementDO = BeanUtil.convertBean(agreementParam, AgreementDO.class);
        agreementDO.setModifyTime(LocalDateTime.now());
        return this.agreementMapper.updateById(agreementDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeStatus(Long id, Integer status, Long modifyBy) {
        AgreementDO agreementDO = new AgreementDO();
        agreementDO.setPublishStatus(status).setModifyBy(modifyBy).setId(id);
        agreementDO.setModifyTime(LocalDateTime.now());
        return this.agreementMapper.updateById(agreementDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        return this.agreementMapper.deleteById(id) > 0;
    }

    @Override
    public Page<AgreementVO> pageList(AgreementPageQuery agreementPageQuery) {
        QueryWrapper<AgreementDO> queryWrapper = new QueryWrapper();
        Page<AgreementDO> page = new Page<>(agreementPageQuery.getPageNum(), agreementPageQuery.getPageSize());
        queryWrapper.lambda().like(StringUtils.isNotBlank(agreementPageQuery.getType()), AgreementDO::getType, agreementPageQuery.getType())
                .like(StringUtils.isNotBlank(agreementPageQuery.getName()), AgreementDO::getName, agreementPageQuery.getName())
                .select(AgreementDO::getName).select(AgreementDO::getType).select(AgreementDO::getId);
        page = this.agreementMapper.selectPage(page, queryWrapper);
        return BeanUtil.convertPage(page, AgreementVO.class);
    }

}
